<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min(1).css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理<small>体检机构地址管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="体检机构名/体检机构" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findByQueryString()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <!--列表中的数据-->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="体检机构名" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="lngandlat" label="经纬度" align="center"></el-table-column>
                <el-table-column prop="image" label="机构图片" align="center"></el-table-column>
                <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页-->
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增体检机构地址" :visible.sync="dialogFormVisible">
                    <template>
                        <el-form label-position="right" label-width="100px">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="体检机构名">
                                        <el-input v-model="formData.name"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="联系电话">
                                        <el-input v-model="formData.telephone"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="上传体检机构地址图片">
                                        <el-upload
                                                class="avatar-uploader"
                                                action="/address/upload.do"
                                                :auto-upload="autoUpload"
                                                name="imgFile"
                                                :show-file-list="false"
                                                :on-success="handleAvatarSuccess"
                                                :before-upload="beforeAvatarUpload">
                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                        </el-upload>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="经纬度">
                                        <el-input v-model="formData.lngandlat"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="详细地址">
                                        <el-input v-model="formData.address"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-button type="primary" icon="el-icon-location-outline" id='open'>设置定位</el-button>
                            </el-row>
                        </el-form>

                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!--编辑标签弹窗-->
            <div class="add-form">
                <el-dialog title="编辑体检机构地址" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-form label-position="right" label-width="100px">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="体检机构名">
                                        <el-input v-model="formData.name"/>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="联系电话">
                                        <el-input v-model="formData.telephone"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="上传体检机构地址图片">
                                        <el-upload
                                                class="avatar-uploader"
                                                action="/address/upload.do"
                                                :auto-upload="autoUpload"
                                                name="imgFile"
                                                :show-file-list="false"
                                                :on-success="handleAvatarSuccess"
                                                :before-upload="beforeAvatarUpload">
                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                        </el-upload>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="经纬度">
                                        <el-input v-model="formData.lngandlat"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="详细地址">
                                        <el-input v-model="formData.address"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-button type="primary" icon="el-icon-location-outline" id='open'>设置定位</el-button>
                            </el-row>
                        </el-form>

                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            smallPagination: {//小型分页相关属性
                smallCurrentPage: 1,
                smallPageSize: 5,
                smallTotal: 0
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//添加表单窗口中检查组列表数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false //编辑页面隐藏
        },
        created() {
            this.findPage();
        },
        methods: {
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            //文件上传成功后的钩子函数，response 为服务端返回的值（因为不是ajax发送的请求，所以直接是服务区响应的数据），
            //file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                //自动提交，action="/setmeal/upload.do"  请求这个路径
                //给imageUrl赋值 ,用于预览展示图片
                //this.imageUrl = 'http://qwwhamo7v.hd-bkt.clouddn.com/a4cf5c7d-81ef-4713-ba90-afd22ee1839003a36073-a140-4942-9b9b-712cecb144901.jpg'
                this.imageUrl = 'http://qwwhchc0u.hd-bkt.clouddn.com/' + response.data;
                this.$message({
                    type: response.flag ? 'success' : 'error',
                    message: response.message
                });
                //给formData中的img赋值，用于往数据库存储
                this.formData.image = response.data;

            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                //比较上传的文件是不是JPEG格式
                const isJPG = file.type === 'image/jpeg';
                //比较文件大小是不是在2M之间
                const isLt2M = file.size / 1024 / 1024 < 2;
                //做判断
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是JPG格式！');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过2MB！');
                }
                return isJPG && isLt2M;

            },

            //添加
            handleAdd() {
                //发送异步请求，添加
                axios.post("/address/add.do?", this.formData).then((res) => {
                    if (res.data.flag) {
                        //弹出提示信息
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        })
                    } else {//执行失败
                        //弹出提示信息
                        this.$message.error(res.data.message);
                    }
                }).finally(() => {
                    //不管成功与失败，都执行分页查询
                    this.findPage();
                    //关闭弹窗
                    this.dialogFormVisible = false;


                })
            },
            //分页查询
            findPage() {
                let param = {
                    //当前页
                    currentPage: this.pagination.currentPage,
                    //每页显示多少条
                    pageSize: this.pagination.pageSize,
                    //查询条件
                    queryString: this.pagination.queryString
                };
                //发送异步请求，分页查询套餐的所有信息
                axios.post("/address/findPage.do", param).then((res) => {
                    //响应的数据给数据模型赋值
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                })
            },
            //小型分页查找
            findSmallPage(row) {
                //异步请求，所需的参数
                let smallParam = {
                    //当前页
                    smallCurrentPage: this.smallPagination.smallCurrentPage,
                    //每页显示多少条
                    smallPageSize: this.smallPagination.smallPageSize,
                };
                //发送异步请求,发送数据 当前页，每页显示多少条，
                axios.post("/checkgroup/findAll.do?checkGroupId=" + row.id, smallParam).then((res) => {
                    //响应的数据赋值给tableData
                    this.tableData = res.data.rows;
                    this.smallPagination.smallTotal = res.data.total;
                })

            },
            // 重置表单
            resetForm() {
                this.$nextTick(function () {
                    this.formData = {};
                })
            },
            // 弹出添加窗口
            handleCreate() {
                //弹出新建窗口
                this.dialogFormVisible = true;
                //每次打开都重置表单数据
                this.resetForm();
                this.imageUrl = null;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //切换小型页码
            handleSmallCurrentChange(currentPage) {
                this.smallPagination.smallCurrentPage = currentPage;
                this.findSmallPage(this.dataList);
            },
            //关键字查询
            findByQueryString() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //编辑弹窗
            handleUpdate(row) {
                //弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                this.formData = row;
            },
            handleEdit() {
                axios.post("/address/edit.do", this.formData).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            type: "success",
                            message: res.data.message
                        })
                    } else {
                        this.$message.error(res.data.message)
                    }
                }).finally(() => {
                    this.findPage();
                    this.dialogFormVisible4Edit = false;
                })
            },
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/address/delete.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                        } else {
                            this.$message.error(res.data.message)
                        }
                    }).finally(()=>{
                        this.findPage();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>
